import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

class _CountMode extends Model {
  int _count = 0;

  get count => _count;

  add() {
    _count++;
    notifyListeners();
  }
}

class ScopedModeDemoPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ScopedModeDemoPageState();
  }
}

class _ScopedModeDemoPageState extends State<ScopedModeDemoPage>
    with SingleTickerProviderStateMixin {
  var _tabctr;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabctr = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ScopedModel<_CountMode>(
      model: _CountMode(),
      child: Scaffold(
        appBar: AppBar(
          title: Text('ScopedModeDemo'),
          bottom: TabBar(
            tabs: [
              Tab(
                text: 'item1',
              ),
              Tab(
                text: 'item2',
              ),
            ],
            controller: _tabctr,
          ),
        ),
        body: TabBarView(
          children: [FirstSubPage(), SecondSubPage()],
          controller: _tabctr,
        ),
        /*floatingActionButton: Builder(builder: (context){
          return FloatingActionButton(
            onPressed: () {
              final model = ScopedModel.of<CountMode>(context);
              model?.add();
            },
            child: Icon(Icons.add),
          );
        }),*/
      ),
    );
  }
}

/*class FirstSubPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ScopedModelDescendant<CountMode>(builder: (context, child, model){
      return Center(
        child: Column(
          children: <Widget>[
            Text('count = ${model.count}', style: Theme.of(context).textTheme.display1,),
            Text('第一种：同界面数据会改动', ),
            Padding(
              padding: EdgeInsets.all(30),
              child: FloatingActionButton(
                onPressed: () => model.add(),
                child: Icon(Icons.add),
              ),
            )
          ],
        ),
      );
    });
  }
}*/

class FirstSubPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _FirstSubPageState();
  }
}

class _FirstSubPageState extends State<FirstSubPage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ScopedModelDescendant<_CountMode>(builder: (context, child, model) {
      return Center(
        child: Column(
          children: <Widget>[
            Text(
              'count = ${model.count}',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            Text(
              '第一种：同界面数据会改动',
            ),
            Padding(
              padding: EdgeInsets.all(30),
              child: FloatingActionButton(
                onPressed: () => model.add(),
                child: Icon(Icons.add),
              ),
            )
          ],
        ),
      );
    });
  }
}

/*class SecondSubPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _SecondSubPageState();
  }
}

class _SecondSubPageState extends State<SecondSubPage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    final model = ScopedModel.of<CountMode>(context);

    return Center(
      child: Column(
        children: <Widget>[
          Text('count = ${model.count}', style: Theme.of(context).textTheme.display1,),
          Text('第二种：同界面数据不会改动', ),
          Padding(
            padding: EdgeInsets.all(30),
            child: FloatingActionButton(
              onPressed: () => model.add(),
              child: Icon(Icons.add),
            ),
          )
        ],
      ),
    );
  }
}*/

class SecondSubPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final model = ScopedModel.of<_CountMode>(context);

    return Center(
      child: Column(
        children: <Widget>[
          Text(
            'count = ${model.count}',
            style: Theme.of(context).textTheme.headlineMedium,
          ),
          Text(
            '第二种：同界面数据不会改动',
          ),
          Padding(
            padding: EdgeInsets.all(30),
            child: FloatingActionButton(
              onPressed: () => model.add(),
              child: Icon(Icons.add),
            ),
          )
        ],
      ),
    );
  }
}
